/********************************************************************************
 * Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
 * All Rights Reserved.
 *
 * File Name     : hal_audio.c
 * Author        : joki.zhu
 * Date          : 2019-09-23
 *
 * Record        :
 * 1.Date        : 2019-09-23
 *   Author      : joki.zhu
 *   Modification: Created file
 ********************************************************************************/
#include "hi_mw_media_intf.h"
#include "app_common.h"

int SampleAudioDataRecvCallBack(HI_HANDLE AencHdl, HI_MW_MEDIA_AUDIO_S* pVStreamData, HI_VOID* pPrivateData)
{
    static FILE *fp = NULL;

    if (fp == NULL) {
        fp = fopen("/sdcard/test.audio", "wb");
        if (fp == NULL) {
            printf("fopen failed\n");
            return -1;
        }
    }
    
	printf("write audio data size %d\n", pVStreamData->u32Len);
    //fwrite(pVStreamData->pu8Addr, pVStreamData->u32Len, 1, fp);
    fwrite(pVStreamData->pu8Addr + 4, pVStreamData->u32Len - 4, 1, fp);
    
    if (fflush(fp))
    {
        perror("fflush file\n");
    }

    return 0;    
}

AV_INT HalAudioAiInit()
{
#ifdef MINI_APP
        return 0;
#endif /* MINI_APP */

    return MW_Media_InitAudioAi(0, 0);
}

AV_INT HalAudioAencStart(AV_HANDLE hAEncHdl, HI_MW_OnAEncDataCB_FN pfnDataCB)
{
#ifdef MINI_APP
        return 0;
#endif /* MINI_APP */

    return HI_MW_AEnc_Start(hAEncHdl, pfnDataCB, NULL);
}

AV_INT HalAudioAencStop(AV_HANDLE hAEncHdl, HI_MW_OnAEncDataCB_FN pfnDataCB)
{
#ifdef MINI_APP
        return 0;
#endif /* MINI_APP */

    return HI_MW_AEnc_Stop(hAEncHdl, pfnDataCB);
}

AV_INT HalAudioAiDeinit()
{
#ifdef MINI_APP
        return 0;
#endif /* MINI_APP */

    return MW_Media_DeinitAudioAi(0, 0);
}

AV_INT HalAudioAoInit(void)
{
#ifdef MINI_APP
        return 0;
#endif /* MINI_APP */

    return MW_Media_InitAudioAo(0);;
}

AV_INT HalAudioAoSendFrame(AV_U8 *pData, AV_INT size)
{    
#ifdef MINI_APP
        return 0;
#endif /* MINI_APP */

    return MW_Media_AoSendFrame(0, pData, size, 1000);;
}

AV_INT HalAudioAoDeinit(void)
{
#ifdef MINI_APP
        return 0;
#endif /* MINI_APP */

    return MW_Media_DeinitAudioAo(0);
}
